/*
 * ResourceConnection.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.interpreter;

import java.io.IOException;
import java.io.InputStream;

import org.scriptme.ecmascript.data.ESLoader;

/**
 * The Class ResourceConnection.
 */
public class ResourceConnection {

	/** The debug. */
	private static boolean debug = false;

	/** The resource. */
	private Object resource; // the resource we are fetching

	/** The cookie. */
	private String cookie; // identification of the loader instance to use

	/** The name. */
	private String name; // name of the resource

	/** The connected. */
	private boolean connected;

	/** The prefix. */
	private final String prefix = LocalClassLoader.urlPrefix;

	/** The prefix length. */
	private final int prefixLength = prefix.length();

	/**
	 * Instantiates a new resource connection.
	 * 
	 * @param url
	 *            the url
	 * 
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 * @throws Exception
	 *             the exception
	 */
	public ResourceConnection(String url) throws IOException, Exception {
		if (ESLoader.isDebugLoader())
			System.out.println(" ** new ResourceConnection('" + url + "')");

		String file = url;
		if (file.startsWith("/")) {
			file = file.substring(1);
		}
		if (!file.startsWith(prefix)) {
			throw new Exception("Resource file should start with /" + prefix);
		}
		cookie = file.substring(prefixLength, file.indexOf("/+/"));
		name = file.substring(file.indexOf("/+/") + 3);
		if (ESLoader.isDebugLoader())
			System.out.println(" ** cookie: " + cookie + ", name: " + name);
	}

	/**
	 * Connect.
	 * 
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public void connect() throws IOException {
		if (ESLoader.isDebugLoader())
			System.out.println(" ** Connect: cookie: " + cookie + ", name: "
					+ name);

		Object o = LocalClassLoader.getLocalResource(cookie, name);
		if (o == null) {
			resource = null;
			return;
		} else {
			resource = o;
		}
	}

	/**
	 * Gets the content.
	 * 
	 * @return the content
	 * 
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public Object getContent() throws IOException {
		if (!connected) {
			connect();
		}
		return resource;
	}

	/**
	 * Gets the input stream.
	 * 
	 * @return the input stream
	 * 
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public InputStream getInputStream() throws IOException {
		if (!connected) {
			connect();
		}
		if (resource instanceof InputStream) {
			return (InputStream) resource;
		}
		return LocalClassLoader.getLocalResourceAsStream(cookie, name);
	}
}
